<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">🔐</div>
        <h1 class="title">{{ systemName }}</h1>
      </div>

      <div class="login-form">
        <div class="form-group">
          <label>
            <span class="label-icon">👤</span>
            用户名
          </label>
          <input v-model="formData.username" type="text" placeholder="请输入管理员用户名" @keyup.enter="handleLogin" autofocus />
        </div>

        <div class="form-group">
          <label>
            <span class="label-icon">🔒</span>
            密码
          </label>
          <input v-model="formData.password" type="password" placeholder="请输入密码" @keyup.enter="handleLogin" />
        </div>

        <button class="login-btn" @click="handleLogin" :disabled="loading">
          {{ loading ? '登录中...' : '登 录' }}
        </button>
      </div>

      <div class="login-footer">
        <p>{{ systemName }} v{{ systemVersion }}</p>
        <p class="copyright">© 2025 All Rights Reserved</p>
      </div>
    </div>

    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, defineEmits } from 'vue';
import { adminLogin } from '../api/admin';
import { getConfig } from '../api/config';

const emit = defineEmits(['login-success']);

const formData = ref({
  username: '',
  password: ''
});

const loading = ref(false);
const systemName = ref('管理后台');
const systemVersion = ref('1.0.3');

// 加载系统配置
const loadConfig = async () => {
  try {
    const res = await getConfig();
    if (res.success) {
      systemName.value = res.data.system_name || '管理后台';
      systemVersion.value = res.data.system_version || '1.0.3';
    }
  } catch (error) {
    console.error('加载配置失败:', error);
  }
};

// 登录
const handleLogin = async () => {
  if (!formData.value.username || !formData.value.password) {
    alert('请输入用户名和密码');
    return;
  }

  loading.value = true;

  try {
    const res = await adminLogin(formData.value);

    if (res.success) {
      // 保存token和管理员信息
      localStorage.setItem('adminToken', res.data.token);
      localStorage.setItem('adminInfo', JSON.stringify(res.data.adminInfo));

      alert('登录成功！');

      // 触发登录成功事件
      emit('login-success');
    } else {
      alert(res.message || '登录失败');
    }
  } catch (error) {
    console.error('登录失败:', error);
    alert('网络错误: ' + (error.message || '请检查网络连接'));
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  // 检查是否已登录
  const token = localStorage.getItem('adminToken');
  if (token) {
    emit('login-success');
  }

  loadConfig();
});
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  top: 60%;
  right: 15%;
  animation-delay: 5s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  bottom: 15%;
  left: 50%;
  animation-delay: 10s;
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }

  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.login-box {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 48px 40px;
  width: 420px;
  max-width: 90%;
  position: relative;
  z-index: 1;
  animation: slideIn 0.5s ease;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  font-size: 64px;
  margin-bottom: 16px;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.subtitle {
  font-size: 16px;
  color: #999;
  margin: 0;
}

.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.label-icon {
  margin-right: 8px;
  font-size: 18px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e5e5e5;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
  box-sizing: border-box;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.login-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 32px;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.tips {
  margin-top: 24px;
  padding: 16px;
  background: #f5f7fa;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.8;
}

.tips p {
  margin: 4px 0;
  color: #666;
}

.tips .warning {
  color: #e23;
  font-weight: 500;
}

.login-footer {
  text-align: center;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #f0f0f0;
}

.login-footer p {
  margin: 4px 0;
  font-size: 13px;
  color: #999;
}

.copyright {
  font-size: 12px !important;
}
</style>
